import type { FormSchemaGetter } from '#/adapter/form';
import type { VxeGridProps } from '#/adapter/vxe-table';

import { productSelect } from '#/api/iot/product';
import Dict from '#/views/iot/product/dict';

import { timeInterval } from './dict';

export const querySchema: FormSchemaGetter = () => [
  {
    component: 'ApiSelect',
    fieldName: 'productKey',
    label: '产品',
    componentProps: () => {
      return {
        valueField: 'productKey',
        labelField: 'productName',
        api: productSelect,
        autoSelect: 'first',
      };
    },
    rules: 'required',
  },
  {
    component: 'Input',
    fieldName: 'deviceSn',
    label: '设备序列号',
  },
  {
    component: 'Input',
    fieldName: 'traceId',
    label: 'TraceID',
  },
  {
    component: 'Select',
    componentProps: {
      options: Dict.eDeviceLogBizType,
    },
    fieldName: 'bizType',
    label: '业务类型',
  },
  {
    component: 'Select',
    fieldName: 'timeInterval',
    label: '时间区间',
    componentProps: {
      fieldNames: { label: 'label', value: 'value' },
      options: timeInterval,
    },
  },
  {
    component: 'RangePicker',
    fieldName: 'timeIntervalRange',
    label: '选择区间',
    componentProps: {
      format: 'YYYY-MM-DD HH:mm:ss',
      showTime: true,
      valueFormat: 'YYYY-MM-DD HH:mm:ss',
      fieldNames: { label: 'label', value: 'value' },
      // options: timeInterval,
    },
    dependencies: {
      if(values) {
        return values.timeInterval === 0;
      },
      // 只有指定的字段改变时，才会触发
      triggerFields: ['timeInterval'],
    },
  },
];

export const columns: VxeGridProps['columns'] = [
  {
    title: '时间',
    field: 'ts',
  },
  {
    title: 'TraceID',
    field: 'trace_id',
  },
  {
    title: '设备序列号',
    field: 'device_sn',
  },
  {
    title: '操作',
    field: 'operation',
  },
  {
    title: '消息内容',
    field: 'message',
  },
  {
    title: '业务类型',
    field: 'biz_type',
    formatter({ cellValue }) {
      return Dict.eDeviceLogBizType.find((item) => item.value === cellValue)
        ?.label as string;
    },
  },
  {
    title: '状态',
    field: 'code',
    resizable: false,
  },
];
